﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Models;
using Sausage.Web.Code;
using Sausage.Web.DataModel;
using Sausage.Web.Domain;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 应急反应控制器。
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2012/03/09 10:45    创建
    /// </history>
    public class EmergencyReactionController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 发布应急反应。
        /// </summary>
        /// <returns>执行结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(EmergencyReactionListRow data)
        {
            EmergencyReactionHelper.CreateEmergencyReaction(data);
            return Json(new JsonResultViewModel { success = true, message = "应急反应发布成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除已经反应
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(Guid reactionID)
        {
            EmergencyReactionHelper.DeleteEmergencyReaction(reactionID);
            return Json(new JsonResultViewModel { success = true, message = "应急反应删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查询应急反应数据。
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {

            var result = EmergencyReactionHelper.Select(query);

            return new JsonNetResult()
                       {
                           Data = new JsonResultViewModel
                                      {
                                          success = true,
                                          data = result.Data,
                                          total = result.Total,
                                          message = "应急反应数据查询成功！"
                                      },
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }

        /// <summary>
        /// 查询最新的应急反应信息。
        /// </summary>
        /// <returns>
        /// 执行结果。
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult LastestEmergencyReaction()
        {
            var entity = EmergencyReactionHelper.GetLastestEmergencyReaction();
            if (entity == null)
                return Json(new JsonResultViewModel { success = true }, JsonRequestBehavior.AllowGet);


            string imageUrl = entity.WeatherWarningUrl;
            if (!string.IsNullOrEmpty(imageUrl) && imageUrl.StartsWith("~")) //internal image url, user also could set a out url.
                imageUrl = Url.Content(imageUrl);

            return new JsonNetResult()
                       {
                           Data = new JsonResultViewModel
                                      {
                                          success = true,
                                          data = new
                                                     {
                                                         ImageUrl = string.IsNullOrEmpty(entity.WeatherWarningUrl) ? null : imageUrl,
                                                         Content = string.Format("天气预警： {0}<br/>响应等级： {1}<br/>预警等级： {2}<br/>发布时间： {3:yyyy年MM月dd日 HH时mm分}",
                                                            entity.WeatherWarning, entity.ReactionLevel, entity.AlarmLevel, entity.PostDate)
                                                     },
                                          total = 1,
                                          message = "应急反应数据查询成功！"
                                      },
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }
    }
}
